import { useUserStore } from "./../stores/user";
import { createRouter, createWebHistory } from "vue-router";
import BooksList from "@/views/BookList/Index.vue";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      name: "BooksList",
      component: BooksList,
    },
    {
      path: "/login",
      name: "Login",
      component: () => import("@/views/Login.vue"),
    },
    {
      path: "/register",
      name: "Register",
      component: () => import("@/views/Register.vue"),
    },
    {
      path: "/user/:id",
      name: "UserInfo",
      component: () => import("@/views/ReaderInfo.vue"),
      meta: {
        needLogin: true,
      },
    },
    {
      path: "/book/:isbn",
      name: "BookInfo",
      component: () => import("@/views/BookInfo/Index.vue"),
    },
    {
      path: "/admin/book/:isbn",
      name: "BookIdInfo",
      component: () => import("@/views/BookInfo/BookIdInfo.vue"),
    },
    {
      path: "/borrowBook",
      name: "BorrowBook",
      component: () => import("@/views/BorrowBook.vue"),
      meta: {
        needLogin: true,
      },
    },
    {
      path: "/reserveBook",
      name: "ReserveBook",
      component: () => import("@/views/ReserveBook.vue"),
      meta: {
        needLogin: true,
      },
    },
    {
      path: "/updatePass",
      name: "UpdatePass",
      component: () => import("@/views/UpdatePass.vue"),
      meta: {
        needLogin: true,
      },
    },
    {
      path: "/admin/manageUser",
      name: "UserList",
      component: () => import("@/views/UserList.vue"),
      meta: {
        needLogin: true,
      },
    },
    {
      path: "/admin/manageBook",
      name: "BookManage",
      component: () => import("@/views/BookManage/Index.vue"),
      meta: {
        needLogin: true,
      },
    },
    {
      path: "/admin/allBorrow",
      name: "AllBorrow",
      component: () => import("@/views/AllBorrow.vue"),
      meta: {
        needLogin: true,
      },
    },
    {
      path: "/history/allReservation",
      name: "AllReservationHistory",
      component: () => import("@/views/HistoryOfAllReservation.vue"),
      meta: {
        needLogin: true,
      },
    },
    {
      path: "/history/myReservation",
      name: "MyReservationHistory",
      component: () => import("@/views/HistoryOfMyReservation.vue"),
      meta: {
        needLogin: true,
      },
    },
    {
      path: "/history/allBorrow",
      name: "AllBorrowHistory",
      component: () => import("@/views/HistoryOfAllBorrow.vue"),
      meta: {
        needLogin: true,
      },
    },
    {
      path: "/history/myBorrow",
      name: "MyBorrowHistory",
      component: () => import("@/views/HistoryOfMyBorrow.vue"),
      meta: {
        needLogin: true,
      },
    },
    {
      path: "/history/allFine",
      name: "AllFineHistory",
      component: () => import("@/views/HistoryOfAllFine.vue"),
      meta: {
        needLogin: true,
      },
    },
    {
      path: "/history/myFine",
      name: "MyFineHistory",
      component: () => import("@/views/HistoryOfMyFine.vue"),
      meta: {
        needLogin: true,
      },
    },
    {
      path: "/check/damageBook",
      name: "checkDamageBook",
      component: () => import("@/views/checkDamageBook.vue"),
      meta: {
        needLogin: true,
      },
    },
    {
      path: "/check/lostBook",
      name: "checkLostBook",
      component: () => import("@/views/checkLostBook.vue"),
      meta: {
        needLogin: true,
      },
    },
    {
      path: "/change/bookCategory",
      name: "changeBookCategory",
      component: () => import("@/views/ChangeBookCategory.vue"),
      meta: {
        needLogin: true,
      },
    },
    {
      path: "/change/bookLocation",
      name: "changeBookLocation",
      component: () => import("@/views/ChangeBookLocation.vue"),
      meta: {
        needLogin: true,
      },
    },
  ],
});

router.beforeEach((to, from, next) => {
  const store = useUserStore();
  if (to.meta.needLogin && !store.isLogin) {
    next("/login");
  } else {
    next();
  }
});
export default router;
